//
//  BKGDGradeView.m
//  BESTKEEP
//
//  Created by 毛韶谦 on 2016/10/8.
//  Copyright © 2016年 YISHANG. All rights reserved.
//

#import "BKGDGradeView.h"

#define BKGDGradeCellGrayImage [[UIImage imageNamed:@"BKGDGradeGray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
#define BKGDGradeCellYellowImage [[UIImage imageNamed:@"BKGDGradeYellow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
#define BKGDGradeMaxValue 5

@interface BKGDGradeViewCell : UICollectionViewCell

@property(nonatomic,strong) UIImageView *imageView;

@end

@implementation BKGDGradeViewCell

-(void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    
    UIImage *image = selected ? BKGDGradeCellYellowImage : BKGDGradeCellGrayImage;
    _imageView.image = image;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self initialize];
    }
    return self;
}

-(void)initialize {
    _imageView = [[UIImageView alloc] init];
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.image = BKGDGradeCellGrayImage;
    
    [self.contentView addSubview:_imageView];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _imageView.frame = self.bounds;
}

@end





NSString *const BKGDGradeViewCellIdentifier = @"BKGDGradeViewCellIdentifier";

@interface BKGDGradeView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong) UICollectionView *collectionView;

@end

@implementation BKGDGradeView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if(self) {
        [self initialize];
    }
    return self;
}

-(void)initialize {
    
    _gradeValue = 0;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.multipleTouchEnabled = YES;
    _collectionView.alwaysBounceHorizontal = NO;
    _collectionView.alwaysBounceVertical = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerClass:[BKGDGradeViewCell class] forCellWithReuseIdentifier:BKGDGradeViewCellIdentifier];
    [self addSubview:_collectionView];
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    _collectionView.frame = self.bounds;
}

#pragma -mark collectionView delegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return BKGDGradeMaxValue;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = CGRectGetWidth(self.frame) / BKGDGradeMaxValue;
    CGSize size = CGSizeMake(width, CGRectGetHeight(self.frame));
    
    return size;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BKGDGradeViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:BKGDGradeViewCellIdentifier forIndexPath:indexPath];
    
    if(indexPath.row < _gradeValue) {
        [cell setSelected:YES];
    } else {
        [cell setSelected:NO];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    _gradeValue = indexPath.row + 1;
    
    [collectionView reloadData];
}

-(void)setAllowSelection:(BOOL)allowSelection {
    _allowSelection = allowSelection;
    self.userInteractionEnabled = _allowSelection;
}

-(void)setGradeValue:(NSUInteger)gradeValue {
    _gradeValue = gradeValue;
    if(_gradeValue > BKGDGradeMaxValue) {
        _gradeValue = BKGDGradeMaxValue;
    }
    
    [_collectionView.collectionViewLayout invalidateLayout];
    [_collectionView reloadData];
}


@end
